
import { positionSelect, positionByEdit } from '@/api/systemManage'
const oldState = { // 用于重置state数据
  tableData: [
    { id: 1, number: '6546545', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清刘有清刘有清刘有清刘有清刘有清刘有清刘有清刘有清刘有清刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    { id: 2, number: '65465456456455645644546546', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    { id: 3, number: '654654564564', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    { id: 4, number: '654654564564', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    { id: 5, number: '654654564564', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    { id: 6, number: '654654564564', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    { id: 7, number: '654654564564', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 }

  ],
  authorList: [
    {
      level1Name: '用户管理',
      checked: false,
      children: [
        { level2Name: '查看用户信息', checked: false },
        { level2Name: '禁用用户账号', checked: true },
        { level2Name: '导出用户表', checked: false },
        { level2Name: '添加用户', checked: false }

      ]
    },
    {
      level1Name: '会议管理',
      checked: false,
      children: [
        { level2Name: '查看会议信息', checked: false },
        { level2Name: '导出会议信息表', checked: true },
        { level2Name: '导出参会人信息表', checked: false },
        { level2Name: '下载会议文档', checked: false }
      ]
    },
    {
      level1Name: '服务管理',
      checked: false,
      children: [
        {
          level2Name: '服务器',
          checked: false,
          children: [
            { level3Name: '查看服务器信息', checked: false },
            { level3Name: '编辑服务器', checked: false },
            { level3Name: '添加服务器', checked: false },
            { level3Name: '删除服务器', checked: false },
            { level3Name: '服务器密码设置', checked: false }
          ]
        },
        {
          level2Name: '用户协议管理',
          checked: false,
          children: [
            { level3Name: '查看协议详情', checked: false },
            { level3Name: '编辑隐私协议', checked: false }
          ]
        }
      ]
    }

  ]

}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  set_authorList (state, data) {
    state.authorList = data
  },

  set_tableData (state, data) {
    state.tableData = data
  },
  // set_level1flag (state, val) {
  //   state.authorList.find(item => item.level1Name === val).flag = !state.authorList.find(item => item.level1Name === val).flag
  // },
  // set_level2flag (state, val) {
  //   const level1 = state.authorList.find(item => item.level1Name === val[0].level1Name).children
  //   const level2 = level1.find(item => item.level2Name === val[1].level2Name)
  //   level2.flag = !level2.flag
  // },
  set_level1Checked (state, val) {
    const list = state.authorList.find(item => item.level1Name === val.level1Name).children
    for (let i = 0; i < list.length; i++) {
      list[i].checked = !val.checked
      if (list[i].children) {
        for (let j = 0; j < list[i].children.length; j++) {
          list[i].children[j].checked = !val.checked
        }
      }
    }
  },
  set_level2Checked (state, val) {
    const level1 = state.authorList.find(item => item.level1Name === val[0].level1Name).children
    const level2 = level1.find(item => item.level2Name === val[1].level2Name)
    level2.checked = !level2.checked
    if (level2.children) {
      level2.children.map(item => {
        item.checked = level2.checked
      })
    }
    const checked = level1.every(item => item.checked === true)
    state.authorList.find(item => item.level1Name === val[0].level1Name).checked = checked
  },

  set_level3Checked (state, val) {
    const level1 = state.authorList.find(item => item.level1Name === val[0].level1Name).children
    const level2 = level1.find(item => item.level2Name === val[1].level2Name).children
    const level3 = level2.find(item => item.level3Name === val[2].level3Name)
    level3.checked = !level3.checked
    const checked = level2.every(item => item.checked === true)
    level1.find(item => item.level2Name === val[1].level2Name).checked = checked
    const allchecked = level1.every(item => item.checked === true)
    state.authorList.find(item => item.level1Name === val[0].level1Name).checked = allchecked
  }
}
const actions = {
  positionSelectData ({ state, commit }) {
    return new Promise((resolve, reject) => {
      positionSelect().then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data.data) {
          resolve(res.data)
          commit('set_authorList', res.data.data)
        } else {
          reject(res.data)
        }
      })
    })
  },
  positionByEditData ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      positionByEdit(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data.data) {
          resolve(res.data)
          commit('set_authorList', res.data.data.systemPermissions)
        } else {
          reject(res.data)
        }
      })
    })
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
